package com.cn.shine.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.shine.common.BaseBean;
import com.cn.shine.pojo.PayType;
import com.cn.shine.service.PayTypeService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Controller  
@RequestMapping("/payType") 
public class PayTypeController {
	
    @Resource  
    private PayTypeService payTypeService;
    
    @RequestMapping("/payTypeList")
    public String payTypeList(){
        return "payTypeList";
    }
    
    @RequestMapping("/addPayType")
    @ResponseBody
    public JSONObject addPayType(PayType payType){
        BaseBean bean = new BaseBean();
        if(StringUtils.isEmpty(payType.getpName())){
            bean.setResult("0");
            bean.setMsg("参数不能为空");
            return JSONObject.fromObject(bean);
        }
        PayType p = new PayType();
        p.setpName(payType.getpName());
        int count = payTypeService.getCountPayTypeList(p);
        if(count>0){
        	  bean.setResult("0");
              bean.setMsg("该商品已存在");
              return JSONObject.fromObject(bean);
        }
        int i = payTypeService.insertSelective(payType);
        if(i==0){
            bean.setResult("0");
            bean.setMsg("添加失败");
        }else{
            bean.setMsg("添加成功");  
            bean.setResult("1");  
        }
        return JSONObject.fromObject(bean);
    }
    
    @RequestMapping("/getPayTypeList")
    @ResponseBody
    public JSONObject getPayTypeList(PayType payType,
    		HttpServletRequest request, HttpServletResponse resp){
        List<PayType> list = payTypeService.getPayTypeList();
        JSONObject jsonObject=new JSONObject();
        Gson gson = new GsonBuilder().create();
        jsonObject.put("total", list.size());
        jsonObject.put("rows", gson.toJson(list));
        return jsonObject;
    }
    
    @RequestMapping(value ="/getPayTypeListByPage",produces = { "text/html;charset=UTF-8" })
    @ResponseBody
    public JSONObject getPayTypeListByPage(PayType payType,String pName){
    	List<PayType> list = payTypeService.getPayTypeListByPage(payType);
        int total = payTypeService.getCountPayTypeList(payType);
    	JSONObject jsonObject=new JSONObject();
        Gson gson = new GsonBuilder().create();
        jsonObject.put("total", total);
        jsonObject.put("rows", gson.toJson(list));
        return jsonObject;
    }
    
    @RequestMapping("/deletePayType")
    @ResponseBody
    public JSONObject deletePayType(@RequestParam(value="id",required=true)int id){
        BaseBean bean = new BaseBean();
        Gson gson = new GsonBuilder().create();
        int i = payTypeService.deleteByPrimaryKey(id);
        if(i==1){
            bean.setResult("1");
            bean.setMsg("删除成功");
        }else{
            bean.setResult("0");
            bean.setMsg("删除失败");
        }
        JSONObject jsonObject = JSONObject.fromObject(bean);
        return jsonObject;
    }
}
